// ============================================================================
//
// Copyright (C) 2009-2011 SC Technology Group. - www.sc.com
//
// ============================================================================
package com.cs.vessel.breakdown.wizards;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.cs.vessel.breakdown.CDOHibernateUtil;
import com.cs.vessel.breakdown.view.NavigationView;
import com.cs.vessel.breakdown.view.providers.ComponentViewContentProvider;
import com.cs.vessel.breakdown.view.providers.ComponentViewLabelProvider;
import com.sc.vessel.info.model.breakdown.Component;

/**
 * DOC liaoqc class global comment. Detailled comment
 */
public class AccidentWizardFirstPage extends WizardPage {

    private Component rootComponent;

    /**
     * DOC liaoqc AccidentNewWizardPage constructor comment.
     * 
     * @param pageName
     */
    protected AccidentWizardFirstPage(String pageName) {
        super(pageName);
        setTitle(Messages.AccidentNewWizard_firstPage_title);
        setDescription(Messages.AccidentNewWizard_firstPage_description);
        setPageComplete(false);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    @Override
    public void createControl(Composite parent) {
        final AccidentNewWizard wizard = (AccidentNewWizard) getWizard();
        Composite composite = new Composite(parent, SWT.NONE);
        composite.setLayout(new GridLayout());
        composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
        // composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));

        TreeViewer viewer = new TreeViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
        viewer.setContentProvider(new ComponentViewContentProvider());
        viewer.setLabelProvider(new ComponentViewLabelProvider());
        viewer.addSelectionChangedListener(new ISelectionChangedListener() {

            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                IStructuredSelection selection = ((IStructuredSelection) event.getSelection());
                Object obj = selection.getFirstElement();
                wizard.getAccident().setComponent((Component) obj);
                setPageComplete(true);
            }
        });
        rootComponent = CDOHibernateUtil.getRootCompomnent();
        viewer.setInput(new Object[] { rootComponent });
        viewer.getTree().setLayoutData(
                new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL
                        | GridData.GRAB_VERTICAL));
        // init the selection
        if (wizard.getSelection() != null) {
            Component component = (Component) wizard.getSelection().getFirstElement();
            Component selectedComponent = NavigationView.getCurrentComponent(rootComponent, component.getItemId());
            viewer.expandAll();
            viewer.collapseToLevel(selectedComponent, TreeViewer.ALL_LEVELS);
            viewer.setSelection(new StructuredSelection(selectedComponent), true);
        }
        setControl(composite);
    }

}
